<?php
	include('include/header.php');
	include("include/commands.php");
        
        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();
            
            //Add the new car now.
            //Get variables.
            $model = $_POST["model"];
            $color = $_POST["color"];
            $year = $_POST["year"];
            $price = $_POST["price"];
           
            //Create the query.
            $query =    "INSERT INTO Inventory (Model, Color, Year, Price, Sold)
                        VALUES (\"$model\", \"$color\", $year, $price,\"Available\")";

            //Insert the tuple.
            $connector->insertQuery($query);            
        }
        
	if(isset($_GET['sortBy']) || isset($_GET['filter'])) {
		if(!isset($_GET['start']) || !isset($_GET['end'])) {
			$inventory = getInventorySorted($_GET['sortBy'], $_GET['model'],$_GET['color'],$_GET['year'],$_GET['filter']);
		}
	}
	else {
		$inventory = getInventory();
	}
?>

	<div class="body">
	
		<h1> Inventory List </h1>

		<div class="border">
			<form>
			<p>
			Model <input type="text" name="model" value=""/><br/>
			Color <input type="text" name="color"/><br/>
			Year <input type="text" name="year"/><br/>
			Sort By:
				<select name="sortBy">
					<option>NONE</option>
					<option>Model</option>
					<option>Color</option>
					<option>Year</option>
					<option>Price</option>
				</select>
				 Filter By: <select name="filter">
										<option></option><option>Sold</option><option>Available</option>
										</select>
			<input type="submit" value="Filter"/>
			</p>
			</form>
		</div>
                <br/>
                <div class="border">
                    <form id="newInventoryForm" action="inventory.php" method="post" onsubmit="return validateInventoryInsert();">
                        <h4>Add a New Car</h4>
                        <p>
                            Model* <input type="text" name="model" id="model"/><br/>
                            Color* <input type="text" name="color" id="color"/><br/>
                            Year* <input type="text" name="year" id="year"/><br/>
                            Price* <input type="text" name="price" id="price"/><br/>
                      
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>
		</br>
		<?php 

			if(sizeof($inventory) > 0){
					generateTransactionsTable($inventory);
				}
				else{
					echo "<p> No Inventory Found. </p>";
				}
				
				function generateTransactionsTable($inventory) {
					echo "<table>";
					echo "<tr>";
					echo "<th>Id</th>";
					echo "<th>Model</th>";
					echo "<th>Color</th>";				
					echo "<th>Year</th>";
					echo "<th>Price($)</th>";
                                        echo "<th>Sold</th>";
					echo "</tr>";
					generateTableBody($inventory);
					echo "</table>";
				}
		?>
        </br>
	</div>


<?php 
	include('include/footer.php'); 
?>